package config;

import java.util.HashMap;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

import controller.AdminController;
import controller.DataAdminController;
import controller.LogController;
import controller.LoginController;
import controller.UserController;
import db.area;
import db.art;
import db.article;
import db.build;
import db.hclog;
import db.model;
import db.oaUser;
import db.pts;
import db.report;
import db.sysuser;
import utils.smartEnv;

public class MainConfig extends JFinalConfig{
	public static Prop p;
	private WallFilter wallFilter;


	public static void loadConfig() {
		if (p == null) {
			p = PropKit.use("config.properties").appendIfExists("config-pro.properties");
		}
	}

	public static DruidPlugin getDruidPlugin() {
		loadConfig();
		return new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password"));
	}
	
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		loadConfig();
		//璁剧疆褰撳墠鏄惁涓哄紑鍙戞ā寮�
		me.setDevMode(p.getBoolean("devMode"));
		//璁剧疆榛樿涓婁紶鏂囦欢淇濆瓨璺緞 getFile绛変娇鐢�
		me.setBaseUploadPath("upload/");
		//璁剧疆涓婁紶鏈�澶ч檺鍒跺昂瀵�
		me.setMaxPostSize(1024*1024*1024);
		//璁剧疆榛樿涓嬭浇鏂囦欢璺緞 renderFile浣跨敤
		//me.setBaseDownloadPath("D:/download");
		me.setBaseDownloadPath("upload/");
		
		//璁剧疆榛樿瑙嗗浘绫诲瀷
		me.setViewType(ViewType.JSP);
		//璁剧疆404娓叉煋瑙嗗浘
		//me.setError404View("404.html");
		
		//璁剧疆鍚敤渚濊禆娉ㄥ叆
		me.setInjectDependency(true);		
		
		smartEnv.fileloc = p.get("filesloc");
		smartEnv.personloc = p.get("personloc");
		
		//璁剧疆鏂囨。绫诲瀷甯搁噺
		smartEnv.docTypeMap = new HashMap<String,String>();
		smartEnv.docTypeMap.put("1", "鐐逛簯");
		smartEnv.docTypeMap.put("2", "3MX妯″瀷");
		smartEnv.docTypeMap.put("3", "OFFICE鏂囨。");
		smartEnv.docTypeMap.put("4", "DWG鏂囨。");
		smartEnv.docTypeMap.put("5", "姝ｅ皠褰卞儚鍥�");
		smartEnv.docTypeMap.put("6", "PDF鏂囨。");
		smartEnv.docTypeMap.put("7", "澶氬眰鐐逛簯");
		smartEnv.docTypeMap.put("10", "BIM妯″瀷");
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		me.setDevMode(p.getBoolean("engineDevMode", false));
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		loadConfig();
		//閰嶇疆鏁版嵁搴撹繛鎺ユ睜鎻掍欢
		DruidPlugin dbPlugin=getDruidPlugin();
		wallFilter = new WallFilter();			// 鍔犲己鏁版嵁搴撳畨鍏�
		wallFilter.setDbType("mysql");
		dbPlugin.addFilter(wallFilter);
		dbPlugin.addFilter(new StatFilter());	// 娣诲姞 StatFilter 鎵嶄細鏈夌粺璁℃暟鎹�
		
		//鏁版嵁鏄犲皠 閰嶇疆ActiveRecord鎻掍欢
		ActiveRecordPlugin arp=new ActiveRecordPlugin(dbPlugin);
		arp.setShowSql(p.getBoolean("devMode"));
		arp.setDialect(new MysqlDialect());
		dbPlugin.setDriverClass("com.mysql.jdbc.Driver");
		/********鍦ㄦ娣诲姞鏁版嵁搴� 琛�-Model 鏄犲皠*********/
		//濡傛灉浣跨敤浜咼Final Model 鐢熸垚鍣� 鐢熸垚浜咮aseModel 鎶婁笅闈㈡敞閲婅В寮�鍗冲彲
		//_MappingKit.mapping(arp);
		
		//娣诲姞鍒版彃浠跺垪琛ㄤ腑
		me.add(dbPlugin);
		me.add(arp);

		arp.addMapping("sys_user", sysuser.class);
		arp.addMapping("art", art.class);
		arp.addMapping("article", article.class);
		arp.addMapping("build", build.class);
		arp.addMapping("report", report.class);
		arp.addMapping("model", model.class);
		arp.addMapping("pts", pts.class);
		arp.addMapping("area", area.class);
		arp.addMapping("log", hclog.class);
		arp.addMapping("sys_oauser", oaUser.class); // 新增
		
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		me.add("enter",LoginController.class);
		me.add("admin",AdminController.class);
		me.add("data",DataAdminController.class); 
		me.add("user",UserController.class);
		me.add("log",LogController.class); 
	}

}
